﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using BSCMFF.Core.DataAccessLayer;
using BSCMFF.Core.Exceptions.Database;
using BSCMFF.Core;
using BSCMFF.Web.UserControls.Selections;

namespace BSCMFF.Web.Pages.Evaluation_Setup
{
    public partial class AssignEmployeeEvaluation : MultiLanguagePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //if (IsPostBack)
            //{
            //    int evalID = Int32.Parse(RadComboBoxEvaluations.SelectedValue);

            //    BSCEntities context = new BSCEntities();

            //    List<User> users = (from eu in context.Evaluation_Evaluator_Employee.Include("User.SBU")
            //                        where eu.Evaluation_FK == evalID //28
            //                        select eu.User).ToList();

            //    RadGridAssignedEmployees.DataSource = users;
            //    RadGridAssignedEmployees.DataBind();
            //}

            if (IsPostBack)
            {
                PopulateAlreadyAssignedEmployees(RadComboBoxEvaluations.SelectedValue);
                //PersistNoManagerSource();
            }
            else
            {
                //Session.Remove("noManagerUsers");
            }
        }

        private void PersistNoManagerSource()
        {
            if (Session["noManagerUsers"] != null)
            {
                RadGridNoManagerEmployees.DataSource = Session["noManagerUsers"];
                RadGridNoManagerEmployees.DataBind();
            }
        }

        protected void RadComboBoxEvaluations_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            PopulateAlreadyAssignedEmployees(e.Value);
        }

        private void PopulateAlreadyAssignedEmployees(string evaluationID)
        {
            int evalID = Int32.Parse(evaluationID);

            if (evalID == 0)
            {
                PanelAlreadyAssignedEmployees.Visible = false;
                PanelAssignNewEmployees.Visible = false;
                EGS.Visible = false;
                return;
            }

            EGS.Visible = true;

            BSCEntities context = new BSCEntities();


            List<User> users = (from eu in context.Evaluation_Evaluator_Employee.Include("User.SBU").Include("User2")
                                where eu.Evaluation_FK == evalID
                                select eu.User).ToList();

            //users[0].ma

            //RadGridAlreadyAssignedEmployees.Items
            if (users.Count != 0)
            {
                RadGridAlreadyAssignedEmployees.DataSource = users;
                RadGridAlreadyAssignedEmployees.DataBind();
                PanelAlreadyAssignedEmployees.Visible = true;
            }
            else
            {
                PanelAlreadyAssignedEmployees.Visible = false;
            }
            //radgri
        }

        protected void RadButtonAssignEmployees_Click(object sender, EventArgs e)
        {
            BSCEntities context = new BSCEntities();

            List<User> previewUsers = new List<User>();
            List<User> noManagerUsers = new List<User>();

            foreach (RadListBoxItem rlbi in EGS.RadListBoxSelectedEmployees.Items)
            {
                int userID = Int32.Parse(rlbi.Value);
                User user = (from u in context.Users
                             where u.ID == userID
                             select u).FirstOrDefault();

                if (user.Manager_FK != null)
                {
                    previewUsers.Add(user);
                }
                else
                {
                    noManagerUsers.Add(user);
                }


            }

            RadGridNoManagerEmployees.DataSource = noManagerUsers;
            RadGridNoManagerEmployees.DataBind();

            if (Session["noManagerUsers"] == null)
            {
                Session.Add("noManagerUsers", noManagerUsers);
            }
            else
            {
                Session["noManagerUsers"] = noManagerUsers;
            }
                

            //TODO: save previewUsers into db
            PanelAssignNewEmployees.Visible = true;


            RadGridToAssignEmployees.DataSource = previewUsers; //previewEEE;
            RadGridToAssignEmployees.DataBind();

            //try
            //{
            //    context.SaveChanges();
            //}
            //catch
            //{
            //    throw new InconsistentDatabaseException("Couldn't assign Employees");
            //}
        }


        protected void RadButtonAssign_Click(object sender, EventArgs e)
        {
            BSCEntities context = new BSCEntities();

            List<Evaluation_Evaluator_Employee> previewEEE = new List<Evaluation_Evaluator_Employee>();

            foreach (GridDataItem gdi in RadGridToAssignEmployees.Items)
            {

                Evaluation_Evaluator_Employee eee = new Evaluation_Evaluator_Employee();
                //eee.Employee_FK = gdi
                eee.Evaluation_FK = Int32.Parse(RadComboBoxEvaluations.SelectedValue);

                User emp = (from u in context.Users
                            where u.ID == eee.Employee_FK
                            select u).FirstOrDefault();

                if (emp.Manager_FK != null)
                {
                    eee.Evaluator_FK = (int)emp.Manager_FK;
                }
                //context.AddToEvaluation_User(eu);
                previewEEE.Add(eee);
            }
        }

        protected void RadGridNoManagerEmployees_ItemCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName == "Edit")
            {
                GridDataItem gdi = (GridDataItem)e.Item;

                //if(gdi["Evaluator_FK"].Text == "")
                if (((EmployeeSimpleSelection)gdi["Evaluator_FK"].FindControl("es")).SelectedUserID != null)
                {

                    int fieldValue = Int32.Parse(gdi["Evaluator_FK"].Text);
                }

                e.Canceled = true;
            }
        }
    }
}